%% The Secant Method

%Basically Newton's method (see Mar_4.m) using numerical derivatives

web('https://en.wikipedia.org/wiki/Secant_method')

%Function that will cross zero
X=(-.5:.1:2)
f=@(x).5-exp(-x)
f_p=@(x)exp(-x)

figure
plot(X,f(X))
xlabel('X')
xlabel('f(X)')

TolF=eps;
clear XN FN

%Setup up starting conditions.  We need an 'n-1' point for the algorithm
X0=0;
X1=.1;
XN(1)=X0;
XN(2)=X1;
FN(1)=f(XN(1)); %We will use an array to store results of function evaluations

Cnt=2;
D=2*TolF
while D>TolF
    
    F_n = f( XN(Cnt) );
    F_n_1 = FN(Cnt-1); %Use the stored value to save a function evaluation
    
    %This is the main update 
    XN(Cnt+1)=XN(Cnt) - F_n * (XN(Cnt)-XN(Cnt-1))/...
        (F_n-F_n_1);
    
    FN(Cnt)=F_n;
    
    D = abs(f(XN(Cnt+1)));
    
    %Plotting
    hold off
    plot(X,f(X))
    hold on
    plot([XN(Cnt),XN(Cnt+1)],f([XN(Cnt),XN(Cnt+1)]),'r','linewidth',5)
    s=sprintf('X: %g F(X): %g',XN(Cnt+1),f(XN(Cnt+1)))
    text(XN(Cnt+1),f(XN(Cnt+1))+.1,s)
    pause(2)
    
    Cnt=Cnt+1; %increment
end

fprintf('Found a zero crossing at: %g after %d iterations\n',XN(end),Cnt)


%% Golden Section Search

web('https://en.wikipedia.org/wiki/Golden_section_search')

%Starting a skeleton....Finish on Mar_9.

XTol=eps;
IntSize=2*eps;

F=zeros(4,1);  %[f1 f2 f3 f4]
X=zeros(4,1);  %[x1 x2 x3 x4]

while IntSize>XTol
    
    if F(3)<F(4) %Then take left interval
        
    else %Take right interval
        
    end

    
    
end








